package cn.com.ui;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import cn.com.BaseActivity;
import cn.com.R;
import cn.com.adapter.LifebkAdapter;
import cn.com.domain.Lifebk;
import cn.com.service.LifebkService;
import cn.com.util.MyToast;
import cn.com.util.StrUrl;
/**
 * 生活百科
 * @author ChonZY
 */
public class LifeBkActivity extends BaseActivity implements OnClickListener{
	private RadioButton rb_dxhy,rb_yi ,rb_foot,rb_zhu,rb_xing,rb_le,rb_tq,rb_slys,rb_ssxw;
	private ListView lv ;
	LifebkService service ; 
	List<Lifebk> lifeBks = null ; // 短信黄页集合
	LifebkAdapter adapter ;
	private RelativeLayout rl_loading ;
	private static final int FINISH_GET_JSON_DXHY = 0;
	protected static final int NET_DXHY_FAIL = 1;
	private Handler myHandler = new Handler(){
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case FINISH_GET_JSON_DXHY:
				lifeBks = (List<Lifebk>) msg.obj;
				adapter = new LifebkAdapter(LifeBkActivity.this, lifeBks);
				lv.setAdapter(adapter);
				lv.setCacheColorHint(0);
				rl_loading.setVisibility(View.GONE);
				break;

			case NET_DXHY_FAIL:
				MyToast.getToast(LifeBkActivity.this, "获取网络数据失败！").show();
				rl_loading.setVisibility(View.GONE);
				break;
			}
			
		};
	};

	private String path;	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.lifebk);
		initUI();
		
	}

	private void initUI() {
		service = new LifebkService();
		lv = (ListView) findViewById(R.id.lv_lifebk);	
		init_RBtn();
		
	}

	private void init_RBtn() {
		rl_loading = (RelativeLayout) findViewById(R.id.rl_lifebk_loading);
		rb_dxhy = (RadioButton) findViewById(R.id.rb_lifebk_dxhy);
		rb_yi = (RadioButton) findViewById(R.id.rb_lifebk_yi);
		rb_foot = (RadioButton) findViewById(R.id.rb_lifebk_foot);
		rb_zhu = (RadioButton) findViewById(R.id.rb_lifebk_zhu);
		rb_xing = (RadioButton) findViewById(R.id.rb_lifebk_xing);
		rb_le = (RadioButton) findViewById(R.id.rb_lifebk_le);
		rb_tq = (RadioButton) findViewById(R.id.rb_lifebk_tq);
		rb_slys = (RadioButton) findViewById(R.id.rb_lifebk_slys);
		rb_ssxw = (RadioButton) findViewById(R.id.rb_lifebk_ssxw);
		
		rb_dxhy.setOnClickListener(this);
		rb_yi.setOnClickListener(this);
		rb_foot.setOnClickListener(this);
		rb_zhu.setOnClickListener(this);
		rb_xing.setOnClickListener(this);
		rb_le.setOnClickListener(this);
		rb_tq.setOnClickListener(this);
		rb_slys.setOnClickListener(this);
		rb_ssxw.setOnClickListener(this);
		
		changeRBbackgroundResouce();
	}
	
	private void changeRBbackgroundResouce() {
		rb_dxhy.setBackgroundResource(R.drawable.slidebar_green);
		path = StrUrl.strUrl + "api/app.aspx?type=getmenulist&id=1";
		rl_loading.setVisibility(View.VISIBLE);
		data_DXHY();
	}

	@Override
	public void onClick(View v) {
		Intent intent = null ;
		switch (v.getId()) {
		case R.id.rb_lifebk_dxhy: //短息黄页
			rb_dxhy.setBackgroundResource(R.drawable.slidebar_green);
			rb_yi.setBackgroundResource(R.drawable.mm_trans);
			rb_foot.setBackgroundResource(R.drawable.mm_trans);
			rb_zhu.setBackgroundResource(R.drawable.mm_trans);
			rb_xing.setBackgroundResource(R.drawable.mm_trans);
			rb_le.setBackgroundResource(R.drawable.mm_trans);
			rb_tq.setBackgroundResource(R.drawable.mm_trans);
			rb_slys.setBackgroundResource(R.drawable.mm_trans);
			rb_ssxw.setBackgroundResource(R.drawable.mm_trans);

			path = StrUrl.strUrl + "api/app.aspx?type=getmenulist&id=1";
			rl_loading.setVisibility(View.VISIBLE);
			data_DXHY();
			
			
			break ;
		case R.id.rb_lifebk_yi:  // 衣
			rb_dxhy.setBackgroundResource(R.drawable.mm_trans);
			rb_yi.setBackgroundResource(R.drawable.slidebar_green);
			rb_foot.setBackgroundResource(R.drawable.mm_trans);
			rb_zhu.setBackgroundResource(R.drawable.mm_trans);
			rb_xing.setBackgroundResource(R.drawable.mm_trans);
			rb_le.setBackgroundResource(R.drawable.mm_trans);
			rb_tq.setBackgroundResource(R.drawable.mm_trans);
			rb_slys.setBackgroundResource(R.drawable.mm_trans);
			rb_ssxw.setBackgroundResource(R.drawable.mm_trans);
			
			path = StrUrl.strUrl+"api/app.aspx?type=getmenulist&id=2";
			rl_loading.setVisibility(View.VISIBLE);
			data_DXHY();
			
			break;
			
		case R.id.rb_lifebk_foot: // 食
			rb_dxhy.setBackgroundResource(R.drawable.mm_trans);
			rb_yi.setBackgroundResource(R.drawable.mm_trans);
			rb_foot.setBackgroundResource(R.drawable.slidebar_green);
			rb_zhu.setBackgroundResource(R.drawable.mm_trans);
			rb_xing.setBackgroundResource(R.drawable.mm_trans);
			rb_le.setBackgroundResource(R.drawable.mm_trans);
			rb_tq.setBackgroundResource(R.drawable.mm_trans);
			rb_slys.setBackgroundResource(R.drawable.mm_trans);
			rb_ssxw.setBackgroundResource(R.drawable.mm_trans);
			
			break ;
			
		case R.id.rb_lifebk_zhu: // 住
			rb_dxhy.setBackgroundResource(R.drawable.mm_trans);
			rb_yi.setBackgroundResource(R.drawable.mm_trans);
			rb_foot.setBackgroundResource(R.drawable.mm_trans);
			rb_zhu.setBackgroundResource(R.drawable.slidebar_green);
			rb_xing.setBackgroundResource(R.drawable.mm_trans);
			rb_le.setBackgroundResource(R.drawable.mm_trans);
			rb_tq.setBackgroundResource(R.drawable.mm_trans);
			rb_slys.setBackgroundResource(R.drawable.mm_trans);
			rb_ssxw.setBackgroundResource(R.drawable.mm_trans);
			
			break ;
		case R.id.rb_lifebk_xing: // 行
			rb_dxhy.setBackgroundResource(R.drawable.mm_trans);
			rb_yi.setBackgroundResource(R.drawable.mm_trans);
			rb_foot.setBackgroundResource(R.drawable.mm_trans);
			rb_zhu.setBackgroundResource(R.drawable.mm_trans);
			rb_xing.setBackgroundResource(R.drawable.slidebar_green);
			rb_le.setBackgroundResource(R.drawable.mm_trans);
			rb_tq.setBackgroundResource(R.drawable.mm_trans);
			rb_slys.setBackgroundResource(R.drawable.mm_trans);
			rb_ssxw.setBackgroundResource(R.drawable.mm_trans);
			
			break ;
		case R.id.rb_lifebk_le: // 乐
			rb_dxhy.setBackgroundResource(R.drawable.mm_trans);
			rb_yi.setBackgroundResource(R.drawable.mm_trans);
			rb_foot.setBackgroundResource(R.drawable.mm_trans);
			rb_zhu.setBackgroundResource(R.drawable.mm_trans);
			rb_xing.setBackgroundResource(R.drawable.mm_trans);
			rb_le.setBackgroundResource(R.drawable.slidebar_green);
			rb_tq.setBackgroundResource(R.drawable.mm_trans);
			rb_slys.setBackgroundResource(R.drawable.mm_trans);
			rb_ssxw.setBackgroundResource(R.drawable.mm_trans);
			
			break ;
		case R.id.rb_lifebk_tq: // 天气
			rb_dxhy.setBackgroundResource(R.drawable.mm_trans);
			rb_yi.setBackgroundResource(R.drawable.mm_trans);
			rb_foot.setBackgroundResource(R.drawable.mm_trans);
			rb_zhu.setBackgroundResource(R.drawable.mm_trans);
			rb_xing.setBackgroundResource(R.drawable.mm_trans);
			rb_le.setBackgroundResource(R.drawable.mm_trans);
			rb_tq.setBackgroundResource(R.drawable.slidebar_green);
			rb_slys.setBackgroundResource(R.drawable.mm_trans);
			rb_ssxw.setBackgroundResource(R.drawable.mm_trans);
			
			break ;
		case R.id.rb_lifebk_slys: // 时令养生
			rb_dxhy.setBackgroundResource(R.drawable.mm_trans);
			rb_yi.setBackgroundResource(R.drawable.mm_trans);
			rb_foot.setBackgroundResource(R.drawable.mm_trans);
			rb_zhu.setBackgroundResource(R.drawable.mm_trans);
			rb_xing.setBackgroundResource(R.drawable.mm_trans);
			rb_le.setBackgroundResource(R.drawable.mm_trans);
			rb_tq.setBackgroundResource(R.drawable.mm_trans);
			rb_slys.setBackgroundResource(R.drawable.slidebar_green);
			rb_ssxw.setBackgroundResource(R.drawable.mm_trans);
			
			break ;
		case R.id.rb_lifebk_ssxw: // 实时新闻
			rb_dxhy.setBackgroundResource(R.drawable.mm_trans);
			rb_yi.setBackgroundResource(R.drawable.mm_trans);
			rb_foot.setBackgroundResource(R.drawable.mm_trans);
			rb_zhu.setBackgroundResource(R.drawable.mm_trans);
			rb_xing.setBackgroundResource(R.drawable.mm_trans);
			rb_le.setBackgroundResource(R.drawable.mm_trans);
			rb_tq.setBackgroundResource(R.drawable.mm_trans);
			rb_slys.setBackgroundResource(R.drawable.mm_trans);
			rb_ssxw.setBackgroundResource(R.drawable.slidebar_green);
			
			break ;

		}
	}

	private void data_DXHY() {
		new Thread(){
			public void run() {
				try {
					String result = service.connServerForResult(path);
					if(result != null){
						lifeBks = service.parseJson_DXHY(result);
						Message msg = new Message();
						msg.what= FINISH_GET_JSON_DXHY;
						msg.obj= lifeBks;
						myHandler.sendMessage(msg);
					}else{
						Message msg = new Message();
						msg.what= NET_DXHY_FAIL;
						myHandler.sendMessage(msg);
					}
					
				} catch (Exception e) {
					Message msg = new Message();
					msg.what= NET_DXHY_FAIL;
					myHandler.sendMessage(msg);
					e.printStackTrace();
				}
				
			};
		}.start();
	}
	

}


	
